/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pluginTable;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.TableColumn;


/**
 * c'est la feuille de saisi. 
 *
 * @author mazid
 */
public class Table extends JTable implements ActionListener{
 
    private ModelTable modele;
    private JPopupMenu popupMenu;
   
    public Table(ModelTable model){
        this.modele = model;
        this.setModel(modele);
        initTable();
        this.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                if(e.getButton()==MouseEvent.BUTTON3){
                     showPopup(e);
                }
            }
        });
    }
                
    private void initTable(){
        setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        setRowSelectionAllowed(true);
        setColumnSelectionAllowed(true);
        setRowHeight(20);
        setAutoResizeMode(0);
        TableColumn column =this.getColumnModel().getColumn(0);
        column.setPreferredWidth(40);
        column.setCellRenderer(this.getTableHeader().getDefaultRenderer());
        for (int i = 1 ; i < this.getModel().getColumnCount() ; i++) {
            column = this.getColumnModel().getColumn(i);
            column.setPreferredWidth(60);
            column.setResizable(true);
        }
        
        
    }
    private void showPopup(MouseEvent e) {
        popupMenu = new JPopupMenu("Selection");
        if (e.isPopupTrigger()) {
            JMenuItem item = new JMenuItem("ajouter");
            item.addActionListener(this);
            item.setActionCommand("ajouter");
            popupMenu.add(item);
            item = new JMenuItem("inserer");
            item.addActionListener(this);
            item.setActionCommand("inserer");
            popupMenu.add(item);
            popupMenu.show(e.getComponent(), e.getX(), e.getY());
        }
}

    @Override
    public void actionPerformed(ActionEvent e) {
       
        if(e.getActionCommand().equals("ajouter")){
            
            
        }
        if(e.getActionCommand().equals("inserer")){
            
        }
    }
    
    //
    public ArrayList<Double> getValeurs(){
        ArrayList<Double> valeurs = new ArrayList<>();
        for(int row:this.getSelectedRows()){
            for(int col:this.getSelectedColumns()){
                valeurs.add(Double.parseDouble(this.getValueAt(row, col).toString()));
            }
        }
        
        return valeurs;
    }
}
